package com.shoes.controller;

import com.alibaba.fastjson.JSON;
import com.shoes.model.ResponseDO;
import com.shoes.service.StoreService;
import com.shoes.utils.LoggerUtil;
import com.shoes.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author asen
 */
@Controller
@RequestMapping(value = "/store", produces = {"application/json;charset=UTF-8"})
public class StoreController {

    private StoreService storeService;

    @RequestMapping(value = "/info", method = RequestMethod.GET)
    @ResponseBody
    public String getStoreInfo(HttpServletRequest request)
            throws ServletException, IOException {
        ResponseDO result;

        try {
            long userId = Long.parseLong(request.getParameter("user_id"));
            long storeId = Long.parseLong(request.getParameter("store_id"));

            result = storeService.getStoreInfo(userId, storeId);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            result = new ResponseDO(ResultCode.PARAMETER_ERROR, null);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResponseDO(ResultCode.GLOBAL_FAILED, null);
        }

        String data = JSON.toJSONString(result);
        LoggerUtil.logger.info(" Response ResultCode: " + result.getResultCode());
        LoggerUtil.logger.info(" Response Content:\n" + data);
        return data;
    }

    @RequestMapping(value = "/coupons", method = RequestMethod.GET)
    @ResponseBody
    public String getCoupons(HttpServletRequest request)
            throws ServletException, IOException {
        ResponseDO result;

        try {
            long storeId = Long.parseLong(request.getParameter("store_id"));

            result = storeService.getCouponsByStoreId(storeId);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            result = new ResponseDO(ResultCode.PARAMETER_ERROR, null);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResponseDO(ResultCode.GLOBAL_FAILED, null);
        }

        String data = JSON.toJSONString(result);
        LoggerUtil.logger.info(" Response ResultCode: " + result.getResultCode());
        LoggerUtil.logger.info(" Response Content:\n" + data);
        return data;
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public String createStore(HttpServletRequest request)
            throws ServletException, IOException {
        ResponseDO result;

        try {
            long userId = Long.parseLong(request.getParameter("user_id"));
            String headPortrait = request.getParameter("head_portrait");
            String storeName = request.getParameter("store_name");

            result = storeService.createStore(userId, headPortrait, storeName);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            result = new ResponseDO(ResultCode.PARAMETER_ERROR, null);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResponseDO(ResultCode.GLOBAL_FAILED, null);
        }

        String data = JSON.toJSONString(result);
        LoggerUtil.logger.info(" Response ResultCode: " + result.getResultCode());
        LoggerUtil.logger.info(" Response Content:\n" + data);
        return data;
    }

    @RequestMapping(value = "/setupStoreInfo", method = RequestMethod.POST)
    @ResponseBody
    public String setupStoreInfo(HttpServletRequest request)
            throws ServletException, IOException {
        ResponseDO result;

        try {
            long userId = Long.parseLong(request.getParameter("user_id"));
            String headPortrait = request.getParameter("head_portrait");
            String storeName = request.getParameter("store_name");
            String wxNum = request.getParameter("wx_num");
            String storeBackground = request.getParameter("store_background");
            String storeAddress = request.getParameter("store_address");

            result = storeService.setupStoreInfo(userId, headPortrait, storeName, wxNum, storeBackground, storeAddress);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            result = new ResponseDO(ResultCode.PARAMETER_ERROR, null);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResponseDO(ResultCode.GLOBAL_FAILED, null);
        }

        String data = JSON.toJSONString(result);
        LoggerUtil.logger.info(" Response ResultCode: " + result.getResultCode());
        LoggerUtil.logger.info(" Response Content:\n" + data);
        return data;
    }

    @RequestMapping(value = "/setupDefaultFreight", method = RequestMethod.POST)
    @ResponseBody
    public String setupDefaultFreight(HttpServletRequest request)
            throws ServletException, IOException {
        ResponseDO result;

        try {
            long storeId = Long.parseLong(request.getParameter("store_id"));
            long  regionId = Long.parseLong(request.getParameter("region_id"));
            int goodsNum = Integer.parseInt(request.getParameter("goods_num"));
            double freight = Double.parseDouble(request.getParameter("freight"));
            int goodsNumAdd = Integer.parseInt(request.getParameter("goods_num_add"));
            double freightAdd = Double.parseDouble(request.getParameter("freight_add"));

            result = storeService.setupDefaultFreight(storeId, regionId, goodsNum, freight, goodsNumAdd, freightAdd);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            result = new ResponseDO(ResultCode.PARAMETER_ERROR, null);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResponseDO(ResultCode.GLOBAL_FAILED, null);
        }

        String data = JSON.toJSONString(result);
        LoggerUtil.logger.info(" Response ResultCode: " + result.getResultCode());
        LoggerUtil.logger.info(" Response Content:\n" + data);
        return data;
    }

    @RequestMapping(value = "/CODToggle", method = RequestMethod.POST)
    @ResponseBody
    public String setCashOnDelivery(HttpServletRequest request)
            throws ServletException, IOException {
        ResponseDO result;

        try {
            long storeId = Long.parseLong(request.getParameter("store_id"));
            int cod = Integer.parseInt(request.getParameter("cod"));

            result = storeService.setCashOnDelivery(storeId, cod);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            result = new ResponseDO(ResultCode.PARAMETER_ERROR, null);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResponseDO(ResultCode.GLOBAL_FAILED, null);
        }

        String data = JSON.toJSONString(result);
        LoggerUtil.logger.info(" Response ResultCode: " + result.getResultCode());
        LoggerUtil.logger.info(" Response Content:\n" + data);
        return data;
    }

    @RequestMapping(value = "/seventhToggle", method = RequestMethod.POST)
    @ResponseBody
    public String setSeventhDaysReturn(HttpServletRequest request)
            throws ServletException, IOException {
        ResponseDO result;

        try {
            long storeId = Long.parseLong(request.getParameter("store_id"));
            int seventh = Integer.parseInt(request.getParameter("seventh"));

            result = storeService.setSeventhDaysReturn(storeId, seventh);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            result = new ResponseDO(ResultCode.PARAMETER_ERROR, null);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResponseDO(ResultCode.GLOBAL_FAILED, null);
        }

        String data = JSON.toJSONString(result);
        LoggerUtil.logger.info(" Response ResultCode: " + result.getResultCode());
        LoggerUtil.logger.info(" Response Content:\n" + data);
        return data;
    }

    @RequestMapping(value = "/guaranteeToggle", method = RequestMethod.POST)
    @ResponseBody
    public String setGuaranteedBusiness(HttpServletRequest request)
            throws ServletException, IOException {
        ResponseDO result;

        try {
            long storeId = Long.parseLong(request.getParameter("store_id"));
            int guarantee = Integer.parseInt(request.getParameter("guarantee"));

            result = storeService.setGuaranteeBusiness(storeId, guarantee);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            result = new ResponseDO(ResultCode.PARAMETER_ERROR, null);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResponseDO(ResultCode.GLOBAL_FAILED, null);
        }

        String data = JSON.toJSONString(result);
        LoggerUtil.logger.info(" Response ResultCode: " + result.getResultCode());
        LoggerUtil.logger.info(" Response Content:\n" + data);
        return data;
    }

    @RequestMapping(value = "/createCoupon", method = RequestMethod.POST)
    @ResponseBody
    public String createCoupon(
            @RequestParam(value = "store_id", required = false)Long storeId,
            @RequestParam(value = "coupon_value", required = false)Double couponValue,
            @RequestParam(value = "start_time", required = false)Long startTime,
            @RequestParam(value = "end_time", required = false)Long endTime,
            @RequestParam(value = "min_amount", required = false)Double minAmount,
            @RequestParam(value = "coupon_num", required = false)Integer couponNum,
            @RequestParam(value = "limit_num", required = false)Integer limitNum)
            throws ServletException, IOException {
        ResponseDO result;

        try {
            if(storeId == null) {
                throw new NullPointerException("storeId is null");
            }
            if(couponValue == null) {
                couponValue = 100d;
            }
            if(minAmount == null) {
                minAmount = 0d;
            }
            if(couponNum == null) {
                couponNum = 1;
            }
            if(limitNum == null) {
                limitNum = 1;
            }

            result = storeService.createCoupon(storeId, couponValue, startTime, endTime, minAmount, couponNum, limitNum);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            result = new ResponseDO(ResultCode.PARAMETER_ERROR, null);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResponseDO(ResultCode.GLOBAL_FAILED, null);
        }

        String data = JSON.toJSONString(result);
        LoggerUtil.logger.info(" Response ResultCode: " + result.getResultCode());
        LoggerUtil.logger.info(" Response Content:\n" + data);
        return data;
    }

    @RequestMapping(value = "/deleteCoupon", method = RequestMethod.POST)
    @ResponseBody
    public String deleteCoupon(HttpServletRequest request)
            throws ServletException, IOException {
        ResponseDO result;

        try {
            long storeId = Long.parseLong(request.getParameter("store_id"));
            String id = request.getParameter("id");

            result = storeService.deleteCoupon(storeId, id);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            result = new ResponseDO(ResultCode.PARAMETER_ERROR, null);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResponseDO(ResultCode.GLOBAL_FAILED, null);
        }

        String data = JSON.toJSONString(result);
        LoggerUtil.logger.info(" Response ResultCode: " + result.getResultCode());
        LoggerUtil.logger.info(" Response Content:\n" + data);
        return data;
    }

    @RequestMapping(value = "/receiveCoupon", method = RequestMethod.POST)
    @ResponseBody
    public String receiveCoupon(HttpServletRequest request)
            throws ServletException, IOException {
        ResponseDO result;

        try {
            long userId = Long.parseLong(request.getParameter("user_id"));
            long storeId = Long.parseLong(request.getParameter("store_id"));
            long couponId = Long.parseLong(request.getParameter("coupon_id"));

            result = storeService.receiveCoupon(userId, storeId, couponId);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            result = new ResponseDO(ResultCode.PARAMETER_ERROR, null);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResponseDO(ResultCode.GLOBAL_FAILED, null);
        }

        String data = JSON.toJSONString(result);
        LoggerUtil.logger.info(" Response ResultCode: " + result.getResultCode());
        LoggerUtil.logger.info(" Response Content:\n" + data);
        return data;
    }

    @RequestMapping(value = "/couponDetail", method = RequestMethod.GET)
    @ResponseBody
    public String getCouponDetail(HttpServletRequest request)
            throws ServletException, IOException {
        ResponseDO result;

        try {
            long storeId = Long.parseLong(request.getParameter("store_id"));

            result = storeService.getCouponDetail(storeId);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            result = new ResponseDO(ResultCode.PARAMETER_ERROR, null);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResponseDO(ResultCode.GLOBAL_FAILED, null);
        }

        String data = JSON.toJSONString(result);
        LoggerUtil.logger.info(" Response ResultCode: " + result.getResultCode());
        LoggerUtil.logger.info(" Response Content:\n" + data);
        return data;
    }

    @RequestMapping(value = "/useCoupon", method = RequestMethod.POST)
    @ResponseBody
    public String useCoupon(HttpServletRequest request)
            throws ServletException, IOException {
        ResponseDO result;

        try {
            long storeId = Long.parseLong(request.getParameter("store_id"));
            long userId = Long.parseLong(request.getParameter("user_id"));
            String couponSn = request.getParameter("coupon_sn");

            LoggerUtil.logger.info("storeId:" + storeId + " userId: " + userId + " couponSn: " + couponSn);

            result = new ResponseDO(ResultCode.GLOBAL_FAILED, null);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            result = new ResponseDO(ResultCode.PARAMETER_ERROR, null);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResponseDO(ResultCode.GLOBAL_FAILED, null);
        }

        String data = JSON.toJSONString(result);
        LoggerUtil.logger.info(" Response ResultCode: " + result.getResultCode());
        LoggerUtil.logger.info(" Response Content:\n" + data);
        return data;
    }

    public StoreService getStoreService() {
        return storeService;
    }
    @Autowired
    public void setStoreService(StoreService storeService) {
        this.storeService = storeService;
    }
}
